# ============================================================================
# 极简GPU测试 - 单个Quad单元静力分析
# ============================================================================

wipe
puts "=== 极简GPU Quad单元测试 ==="

model basic -ndm 2 -ndf 2

# 创建简单的单元网格 2x1m
node 1  0.0  0.0
node 2  1.0  0.0
node 3  1.0  1.0
node 4  0.0  1.0

# 混凝土材料: E=30GPa, nu=0.2
nDMaterial ElasticIsotropic 1  30.0e9  0.2

puts "材料定义: E=30GPa, nu=0.2"

# Quad单元
element quad 1  1 2 3 4  0.2  "PlaneStress"  1

puts "创建Quad单元 1"

# 边界条件: 底部完全固定
fix 1  1  1
fix 2  1  1

# 顶部水平约束
fix 4  1  0
fix 3  1  0

# 简单载荷: 节点3垂直向下1kN
pattern Plain 1 Linear {
    load 3  0.0  -1000.0
}

puts "边界: 底部固定, 顶部可垂直位移"
puts "载荷: 节点3垂直-1000N"

# ========== CPU参考分析 ==========
puts "\n=== CPU分析 (参考) ==="

constraints Plain
numberer Plain
system BandGeneral
test NormDispIncr 1.0e-8 10
algorithm Newton
integrator LoadControl 1.0
analysis Static

set ok [analyze 1]

if {$ok == 0} {
    puts "✓ CPU分析成功"
    set disp_cpu [nodeDisp 3 2]
    puts "  节点3 Y位移(CPU): $disp_cpu m"
} else {
    puts "✗ CPU分析失败"
}

# 保存CPU结果
set cpu_disp [nodeDisp 3 2]

wipe

# ========== 重建模型用于GPU ==========
puts "\n=== 重建模型用于GPU分析 ==="

model basic -ndm 2 -ndf 2

node 1  0.0  0.0
node 2  1.0  0.0
node 3  1.0  1.0
node 4  0.0  1.0

nDMaterial ElasticIsotropic 1  30.0e9  0.2
element quad 1  1 2 3 4  0.2  "PlaneStress"  1

fix 1  1  1
fix 2  1  1
fix 4  1  0
fix 3  1  0

pattern Plain 1 Linear {
    load 3  0.0  -1000.0
}

# ========== GPU分析 ==========
puts "\n=== GPU分析 (启用详细调试) ==="

constraints Plain
numberer Plain
system GPUSolver -debugLevel 2 -enableModule matrixAssembly
test NormDispIncr 1.0e-8 10
algorithm Newton
integrator LoadControl 1.0
analysis Static

puts "运行GPU分析..."
set ok [analyze 1]

if {$ok == 0} {
    puts "✓ GPU分析成功"
    set disp_gpu [nodeDisp 3 2]
    puts "  节点3 Y位移(GPU): $disp_gpu m"

    # 对比CPU vs GPU
    set diff [expr abs($disp_gpu - $cpu_disp)]
    set rel_err [expr abs($diff / $cpu_disp) * 100.0]

    puts "\n=== CPU vs GPU对比 ==="
    puts "  CPU: $cpu_disp m"
    puts "  GPU: $disp_gpu m"
    puts "  差值: $diff m"
    puts "  相对误差: [format %.4f $rel_err]%"

    if {$rel_err < 0.01} {
        puts "  ✓ 精度优秀 (<0.01%)"
    } elseif {$rel_err < 1.0} {
        puts "  ✓ 精度良好 (<1%)"
    } else {
        puts "  ⚠ 精度较差 (>1%)"
    }
} else {
    puts "✗ GPU分析失败"
    puts "  错误代码: $ok"
    puts "\n请检查上方GPU调试输出中的错误信息"
}

puts "\n=== 测试完成 ==="
wipe
